/*
 * Copyright 2014 Hieu Rocker
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rockerhieu.emojicon;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.rockerhieu.emojicon.emoji.SerializeManager;

import java.util.List;

/**
 * @author Hieu Rocker (rockerhieu@gmail.com).
 */
public class EmojiconTextView extends TextView {
    private int mEmojiconSize;
    private int mEmojiconTextSize;
    private int mTextStart = 0;
    private int mTextLength = -1;
    private boolean mUseSystemDefault = false;
    private boolean isLink=false;
    private int mEmojiconLines=0;
    Context context;
    public EmojiconTextView(Context context) {
        super(context);
        this.context=context;
        init(null);
    }

    public EmojiconTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        init(attrs);
    }

    public EmojiconTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context=context;
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        mEmojiconTextSize = (int) getTextSize();
        if (attrs == null) {
            mEmojiconSize = (int) getTextSize();
//            mEmojiconSize = 55;
        } else {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.Emojicon);
            mEmojiconSize = (int) a.getDimension(R.styleable.Emojicon_emojiconSize, getTextSize());
            isLink= a.getBoolean(R.styleable.Emojicon_emojiconLink, false);
            mEmojiconLines= a.getInteger(R.styleable.Emojicon_emojiconLines, 0);
//            mEmojiconSize = 55;
            mTextStart = a.getInteger(R.styleable.Emojicon_emojiconTextStart, 0);
            mTextLength = a.getInteger(R.styleable.Emojicon_emojiconTextLength, -1);
            mUseSystemDefault = a.getBoolean(R.styleable.Emojicon_emojiconUseSystemDefault, false);
            a.recycle();
        }
        setText(getText());
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        if (!TextUtils.isEmpty(text)) {
            SpannableStringBuilder builder = new SpannableStringBuilder(text);
            EmojiconHandler.addEmojis(getContext(), builder, mEmojiconSize, mEmojiconTextSize, mTextStart, mTextLength, mUseSystemDefault);
            text = builder;
        }
        super.setText(text, type);
    }
    public void setText(String content,Intent intent2) {
//        CharSequence sequence = Html.fromHtml(content);
        SpannableStringBuilder strBuilder = new SpannableStringBuilder(content);
        if (content.equals("")) {
        } else {

            //		SpannableString ss = new SpannableString(content);
            List<String> tempL = SerializeManager.getUrlsInContent(content);
            if(tempL != null){
                for(String temp : tempL){
                    int index = content.indexOf(temp);
                    strBuilder.setSpan(new URLClickSpan(temp,intent2), index, index+temp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }

            setText(strBuilder);
            setMovementMethod(LinkMovementMethod.getInstance());

        }
    }
    private class URLClickSpan extends URLSpan {
        String text;
        Intent intent;
        public URLClickSpan(String text,Intent intent) {
            super(text);
            this.text = text;
            this.intent=intent;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(Color.parseColor("#000000"));
            ds.setUnderlineText(true); //ȥ���»���
        }

        @Override
        public void onClick(View widget) {
            //??????????????
            if(null!=intent){
                intent.putExtra("url", text);
                context.startActivity(intent);
            }
        }
    }

    /**
     * ellipsize 属性重写
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        CharSequence charSequence = getText() ;
        if(mEmojiconLines>0&&this.getLineCount()>mEmojiconLines){
            int lastCharDown = getLayout().getLineVisibleEnd(mEmojiconLines-1) ;
            if (charSequence.length() > lastCharDown){
                SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder() ;
                spannableStringBuilder.append(charSequence.subSequence(0,lastCharDown-4)).append("...") ;
                setText(spannableStringBuilder);
            }
        }
        super.onDraw(canvas);
    }
    /**
     * Set the size of emojicon in pixels.
     */
    public void setEmojiconSize(int pixels) {
        mEmojiconSize = pixels;
        super.setText(getText());
    }

    /**
     * Set whether to use system default emojicon
     */
    public void setUseSystemDefault(boolean useSystemDefault) {
        mUseSystemDefault = useSystemDefault;
    }

}
